package com.bot.nft.module.mb.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bot.common.chainbo.ChainAccountInfo;
import com.bot.common.pojo.CommonResult;
import com.bot.common.util.CommonCryptogramUtil;
import com.bot.dev.api.DevBlockChainApi;
import com.bot.nft.core.enums.cm.Enum;
import com.bot.nft.core.enums.mb.MbEnum;
import com.bot.nft.module.mb.domain.bo.MbUserInfoBo;
import com.bot.nft.module.mb.domain.vo.MbUserInfoVo;
import com.bot.nft.module.mb.domain.vo.MemberQueryVo;
import com.bot.nft.module.mb.service.IMbMemberService;
import com.bot.nft.core.config.MinioUtil;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.PutObjectArgs;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 描述: 会员信息控制层
 * <p>
 * 时间: 2023-12-04 21:08
 * 负责人: 卢梦丽
 **/
@CrossOrigin
@RestController
@RequestMapping("/mb")
public class MemberController {

    @Resource
    private IMbMemberService iMbMemberService;

    @Resource
    private DevBlockChainApi devBlockChainApi;

    /**
     * 添加会员
     *
     * @param mbMemberInfoBo 会员信息
     * @return
     */
    @PostMapping("/addMember.do")
    public CommonResult<Boolean> addMemberMbInfo(@RequestBody MbUserInfoBo mbMemberInfoBo) {
        String nickname = mbMemberInfoBo.getNickname();
        Integer userNameNum = iMbMemberService.userNameNum(nickname);//用户名
        String phone = mbMemberInfoBo.getPhone();
        String phoneSm4 = CommonCryptogramUtil.doSm4CbcEncrypt(phone);//手机号加密SM4
        mbMemberInfoBo.setPhone(phoneSm4);
        Integer phoneNum = iMbMemberService.phoneNum(phoneSm4);
        String password = MbEnum.Password.getName();

        String pwdMd5 = DigestUtils.md5DigestAsHex(password.getBytes());
        mbMemberInfoBo.setPassword(pwdMd5);

        if (userNameNum > 0) {
            return CommonResult.error("用户名已存在");
        }
        if (phoneNum > 0) {
            return CommonResult.error("手机号已存在");
        } else {
            ChainAccountInfo chainAccountInfo = devBlockChainApi.creatBlockChain();
            String hexAddress = chainAccountInfo.getHexAddress();//以太坊地址
            String nativeAddress = chainAccountInfo.getNativeAddress();//文昌链地址
            mbMemberInfoBo.setHexAddress(hexAddress);
            mbMemberInfoBo.setNativeAddress(nativeAddress);
            Boolean addMemberMbInfo = iMbMemberService.addMemberMbInfo(mbMemberInfoBo);
            if (!addMemberMbInfo) {
                return CommonResult.error("添加失败");
            }
        }


        return CommonResult.ok();
    }

    /**
     * 修改会员
     *
     * @param mbMemberInfoBo
     * @return
     */
    @PostMapping("/editMember.do")
    public CommonResult<Boolean> updateMemberMbInfo(@RequestBody @Valid MbUserInfoBo mbMemberInfoBo) {

        Boolean updateMemberMbInfo = iMbMemberService.updateMemberMbInfo(mbMemberInfoBo);
        if (!updateMemberMbInfo) {
            return CommonResult.error();
        }

        return CommonResult.ok();
    }

    /**
     * 查看会员
     *
     * @param id
     * @return memberMbInfo
     */
    @GetMapping("/{id}/getMember.do")
    public CommonResult<MbUserInfoVo> getMemberMbInfo(@PathVariable("id") String id) {

        MbUserInfoVo memberMbInfo = iMbMemberService.getMemberMbInfo(id);
        return CommonResult.data(memberMbInfo);

    }

    /**
     * 删除会员
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/delMember.do")
    public CommonResult<Boolean> delMemberMbInfo(@PathVariable("id") String id) {

        Boolean delMemberMbInfo = iMbMemberService.delMemberMbInfo(id);
        if (!delMemberMbInfo) {
            return CommonResult.error();
        }

        return CommonResult.ok();

    }

    /**
     * 分页查询会员信息
     *
     * @param memberQueryBo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @PostMapping("/{pageNo}/{pageSize}/listMember.do")
    public CommonResult<Page<MbUserInfoVo>> queryAuthorByPage(@RequestBody MemberQueryVo memberQueryBo, @PathVariable("pageNo") Integer pageNo, @PathVariable("pageSize") Integer pageSize) {
        Page<MbUserInfoVo> mbMemberInfoVoPageInfo = iMbMemberService.queryMemberByPage(memberQueryBo.getKeyword(), memberQueryBo.getUserStatus(), pageNo, pageSize);
        return CommonResult.data(mbMemberInfoVoPageInfo);
    }

    /**
     * 启用会员
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/startMember.do")
    public CommonResult<Boolean> startMemberMbInfo(@PathVariable("id") String id) {

        Boolean startMemberMbInfo = iMbMemberService.startMemberMbInfo(id);
        if (!startMemberMbInfo) {
            return CommonResult.error();
        }

        return CommonResult.ok();

    }

    /**
     * 禁用会员
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/disMember.do")
    public CommonResult<Boolean> disMemberMbInfo(@PathVariable("id") String id) {

        Boolean disMemberMbInfo = iMbMemberService.disMemberMbInfo(id);
        if (!disMemberMbInfo) {
            return CommonResult.error();
        }

        return CommonResult.ok();

    }

    /**
     * 注册会员
     *
     * @param mbMemberInfoBo 用户信息
     * @return 成功/失败
     */
    @PostMapping("/regAccount.do")
    public CommonResult<Boolean> regAccountMbInfo(@RequestBody MbUserInfoBo mbMemberInfoBo) {
        Boolean regAccountMbInfo = iMbMemberService.regAccountMbInfo(mbMemberInfoBo);

        if (!regAccountMbInfo) {
            return CommonResult.error();
        }

        return CommonResult.ok();

    }





}
